<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace addons\resources\model;

/**
 * Description of ShareVideoRecord
 * 视频分享记录表
 * @author shilinqing
 */
class ShareVideoRecord extends \web\common\model\BaseModel {
    
    protected function _initialize() {
        $this->tableName = 'share_video_record';
    }
    
    /**
     * 查询用户当日分享记录
     * @param type $user_id
     * @return type
     */
    public function userDailyShare($user_id, $video_id, $daily_share_count ,$share_video_send){
        $end_time = date('Y-m-d '.'23:59:59');
        $where['user_id'] = $user_id;
        $where['update_time'] = array('<=',$end_time);
        $record = $this->where($where)->find();
//        dump($record);exit;
        if(!empty($record)){
            $video_ids = explode(',', $record['video_ids']);
            if($record['count'] >= $daily_share_count || in_array($video_id, $video_ids)){
               return 0;
            }
            $record['video_ids'] = $record['video_ids'] .','.$video_id;
            $record['count'] += 1;
            $record['amount'] = $record['amount'] + $share_video_send;
        }else{
            $record['user_id'] = $user_id;
            $record['video_ids'] = $video_id;
            $record['amount'] = $share_video_send;
        }
        $record['update_time'] = NOW_DATETIME;
        $ret = $this->save($record);
        return $ret;
    }

}
